package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ClassifyBean;

import org.xutils.x;

import java.util.List;

/**
 * Created by admin on 2016/12/5.
 */
public class ClassifyAdapter extends RecyclerView.Adapter<ClassifyAdapter.ClassifyViewHolder> implements View.OnClickListener {
    private Context context;
    private List<ClassifyBean> list;


    public ClassifyAdapter(Context context, List<ClassifyBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ClassifyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.classify_item, parent, false);
        ClassifyViewHolder holder = new ClassifyViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(ClassifyViewHolder holder, int position) {

        x.image().bind(holder.img, list.get(position).getPic());
        holder.name.setText(list.get(position).getTitle());
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    class ClassifyViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        TextView name;

        public ClassifyViewHolder(View view) {
            super(view);
            img = (ImageView) view.findViewById(R.id.iv_img);
            name = (TextView) view.findViewById(R.id.tv_name);
        }
    }

    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int data);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


}
